package com.food.restaurantpro.managingnetwork;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

public class ManagingNetwork {
	// Whether there is a Wi-Fi connection.
	private static boolean wifiConnected = false;
	// Whether there is a mobile connection.
	private static boolean mobileConnected = false;
	// The BroadcastReceiver that tracks network connectivity changes.
	private NetworkReceiver receiver;

	private Context s_Context;
	private static ManagingNetwork uniqueInstance;

	private ManagingNetwork(Context context) {
		s_Context = context;
		// Register BroadcastReceiver to track connection changes.
		IntentFilter filter = new IntentFilter(
				ConnectivityManager.CONNECTIVITY_ACTION);
		receiver = new NetworkReceiver();
		s_Context.registerReceiver(receiver, filter);

		updateConnectedFlags(context);
	}

	// Checks the network connection and sets the wifiConnected and
	// mobileConnected variables accordingly.
	private static void updateConnectedFlags(Context context) {
		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
		if (activeInfo != null && activeInfo.isConnected()) {
			wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
			mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
		} else {
			wifiConnected = false;
			mobileConnected = false;
		}

		Log.i("ManagingNetwork.updateConnectedFlags", "wifiConnected = "
				+ wifiConnected + ", mobileConnected = " + mobileConnected);
	}

	/**
	 * After called registerReceiver, this method is used to check network
	 * @return true: network available, else not available
	 */
		public static boolean isNetworkAvailable() {
			if(uniqueInstance != null) {
				if (wifiConnected == true || mobileConnected == true) {
					return true;
				} else {
					return false;
				}
			} else {
				Log.e("RestaurantApp", "Error at class ManagingNetwork: isNetworkAvailable(). You did not call registerReceiver method before call isNetworkAvailable()");
				return false;
			}
		}

		// phuong thuc nay phai duoc goi trong onCreate cua activity dau tien
		/**
		 * register BroadcastReceiver to manage network
		 */
		public static void registerReceiver(Context context) {
			if (uniqueInstance == null) {
				uniqueInstance = new ManagingNetwork(context);
			}
		}

		// Phuong thuc nay phai duoc goi trong onDestroy cua activity exit App (MainActivity)
		/**
		 * unregister BroadcastReceiver which manage network
		 */
		public static void unRegisterReceiver() {
			if (uniqueInstance != null) {
				if (uniqueInstance.receiver != null) {
					uniqueInstance.s_Context.unregisterReceiver(uniqueInstance.receiver);
				}
				uniqueInstance = null;
			}
		}
	/**
	 * 
	 * This BroadcastReceiver intercepts the
	 * android.net.ConnectivityManager.CONNECTIVITY_ACTION, which indicates a
	 * connection change. It checks whether the type is TYPE_WIFI. If it is, it
	 * checks whether Wi-Fi is connected and sets the wifiConnected flag in the
	 * main activity accordingly.
	 * 
	 */
	public class NetworkReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			ConnectivityManager connMgr = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
			if (networkInfo != null && networkInfo.isConnected()) {

				wifiConnected = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
				mobileConnected = networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
				
				Toast.makeText(context, "Wi-Fi reconnected.",
						Toast.LENGTH_SHORT).show();
			} else {
				wifiConnected = false;
				mobileConnected = false;
				Toast.makeText(context, "Lost connection.", Toast.LENGTH_SHORT)
						.show();
			}
			Log.i("NetworkReceiver", "wifiConnected = " + wifiConnected
					+ ", mobileConnected = " + mobileConnected);
		}
	}
}
